<?php
class UsersController extends AppController {
	
	var $name = 'Users';
	
	function login() {
		if(!empty($this->data)) {
			$user = $this->User->findByEmail($this->data['User']['email']);
			$this->set('data', $this->data);
			if($user['User']['password'] == md5($this->data['User']['password'])) {
				unset($user['User']['password']);
				$this->Session->write('user_data', $user);
				$this->Session->setFlash('You have logged in');
				$this->redirect(array('controller' => 'projects', 'action' => 'add'));
			} else {
				$this->set('loginFailed', TRUE);
			}
		}
	}
	
	function logout() {
		if($this->Session->check('user_data')) {
			if($this->Session->delete('user_data')) {
				$this->Session->setFlash('You have been successfully logged out');
				$this->redirect(array('controller' => 'pages', 'action' => 'home'));
			}
		}
	}
	
	function signup() {
		if(!empty($this->data)) {
			if($this->User->findByEmail($this->data['User']['email'])) {
				$this->set('duplicateEmail', TRUE);
			} else {
				$this->data['User']['password'] = md5($this->data['User']['password']);
				if($this->User->Save($this->data)) {
					$this->Session->setFlash("User Sign Up complete. Please Login to continue.");
					$this->redirect(array('controller' => 'users', 'action' => 'login'));
				}
			}
		}
	}
	
}
?>